﻿using System.Collections;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using SellAvi.Services;

namespace SellAvi.ViewModel
{
    public class CustomParamsViewModel : ViewModelBase
    {
        public RelayCommand TruncateAllParamsCommand { get; set; }
        public RelayCommand DeleteParamCommand { get; set; }
        public RelayCommand ReBuildParamsTreeCommand { get; set; }
        public RelayCommand<TreeItem> ParamsSelectionChangedCommand { get; set; }
        public IDataAccessService ServiceProxy { get; set; }
        public ICollectionView ParamsTree { get; set; }
        private TreeItem _selectedTreeItem;

        public bool HasSelectedTreeItem
        {
            get
            {
                if (_selectedTreeItem != null && ParamsTree.Contains(_selectedTreeItem)) return true;
                return false;
            }
        }

        public CustomParamsViewModel(IDataAccessService servPxy)
        {
            ServiceProxy = servPxy;
            ReBuildParamsTree();
            ParamsTree.GroupDescriptions.Add(new PropertyGroupDescription("CAT"));
            TruncateAllParamsCommand = new RelayCommand(TruncateAllParams);
            DeleteParamCommand = new RelayCommand(DeleteSelectedTreeItemParam);
            ReBuildParamsTreeCommand = new RelayCommand(ReBuildParamsTree);
            ParamsSelectionChangedCommand = new RelayCommand<TreeItem>(ParamsSelectionChanged);
        }

        private void ParamsSelectionChanged(TreeItem item)
        {
            _selectedTreeItem = item;
            RaisePropertyChanged("HasSelectedTreeItem");
        }

        private void DeleteSelectedTreeItemParam()
        {
            ServiceProxy.DeleteCustomParam(_selectedTreeItem.ID);
            ReBuildParamsTree();
            ReBuildParamsTree();
        }

        private void TruncateAllParams()
        {
            var d =
            MessageBox.Show(
            "Очистка журнала может привести к невозможности публикации товаров в автоматическом режиме, продолжить?",
            "Очистка журнала", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (d == MessageBoxResult.Yes)
            {
                ServiceProxy.TruncateAllCustomParams();
                ReBuildParamsTree();
            }

        }

        private void ReBuildParamsTree()
        {
            IList itree = (from ctg in ServiceProxy.GetCategories
                           join prm in ServiceProxy.GetParams on ctg.Id equals prm.CategoryId
                           join cp in ServiceProxy.GetCustomParams() on prm.Id equals cp.TParam_Id
                           select new TreeItem()
                            {
                                CAT = ctg.CategoryName,
                                PRM = prm.ParamName,
                                ID = cp.Id,
                                CID = cp.CustomParamValue,
                                CVL = cp.CustomParamId
                            }).ToList();
            ParamsTree = new ListCollectionView(itree);
            RaisePropertyChanged("ParamsTree");
            RaisePropertyChanged("HasSelectedTreeItem");
        }
        public class TreeItem
        {
            public string CAT { get; set; }
            public string PRM { get; set; }
            public int ID { get; set; }
            public int CID { get; set; }
            public int CVL { get; set; }
        }

    }
}
